/**
 * HttpException异常基类
 */
class HttpException extends Error {
  constructor(obj) {
    super()
    this.code = 500
    this.errMsg = '服务器发生了一个未知错误'
    this.errCode = 50001
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 成功
class Success extends HttpException {
  constructor(obj) {
    super()
    this.code = 200
    this.errMsg = 'ok'
    this.errCode = 0
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 失败
class Failed extends HttpException {
  constructor(obj) {
    super()
    this.code = 400
    this.errMsg = '失败'
    this.errCode = 40000
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 认证失败
class AuthFailed extends HttpException {
  constructor(obj) {
    super()
    this.code = 401
    this.errMsg = '认证失败'
    this.errCode = 40001
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 资源未找到
class NotFound extends HttpException {
  constructor(obj) {
    super()
    this.code = 404
    this.errMsg = '资源未找到'
    this.errCode = 40004
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 参数错误
class ParamsException extends HttpException {
  constructor(obj) {
    super()
    this.code = 400
    this.errMsg = '参数错误'
    this.errCode = 40010
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// token失效
class InvalidTokenException extends HttpException {
  constructor(obj) {
    super()
    this.code = 401
    this.errMsg = 'token令牌失效'
    this.errCode = 40002
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// token令牌过期
class ExpiredTokenException extends HttpException {
  constructor(obj) {
    super()
    this.code = 422
    this.errMsg = 'token令牌过期'
    this.errCode = 40022
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 字段重复
class RepeatException extends HttpException {
  constructor(obj) {
    super()
    this.code = 400
    this.errMsg = '字段重复'
    this.errCode = 40060
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 不可操作
class Forbidden extends HttpException {
  constructor(obj) {
    super()
    this.code = 403
    this.errMsg = '不可操作'
    this.errCode = 40030
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 请求方法不允许
class MethodNotAllowed extends HttpException {
  constructor(obj) {
    super()
    this.code = 405
    this.errMsg = '请求方法不允许'
    this.errCode = 40050
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// refresh token 获取失败
class RefreshException extends HttpException {
  constructor(obj) {
    super()
    this.code = 401
    this.errMsg = 'refresh token 获取失败'
    this.errCode = 40009
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}
// 请求过于频繁，请稍后重试
class LimitException extends HttpException {
  constructor(obj) {
    super()
    this.code = 401
    this.errMsg = '请求过于频繁，请稍后重试'
    this.errCode = 40111
    if (obj && obj.code) {
      this.code = obj.code;
    }
    if (obj && obj.errMsg) {
      this.errMsg = obj.errMsg;
    }
    if (obj && obj.errCode) {
      this.errCode = obj.errCode;
    }
  }
}

module.exports = {
  HttpException,
  Success,
  Failed,
  AuthFailed,
  NotFound,
  ParamsException,
  InvalidTokenException,
  ExpiredTokenException,
  RepeatException,
  Forbidden,
  MethodNotAllowed,
  RefreshException,
  LimitException
}
